INSERT overwrite TABLE jms_audit_info.dm_ranger_access_frequency_dt partition(dt)
SELECT requser,
       collect_set(tblname) unique_tbls,
       max(later_access_time) latest_time,
       min(early_access_time) earist_time,
       "userDim" AS dimType,
       '{{ execution_date | cst_ds_nodash }}'
FROM
  (SELECT requser,
          tblname,
          max(evttime) later_access_time,
          min(evttime) early_access_time
   FROM
     (SELECT requser,
             evttime,
             tblname
      FROM jms_audit_info.ranger_audit_info_dt
      WHERE dt='{{ execution_date | cst_ds_nodash }}' ) tt
   GROUP BY requser,
            tblname) tt
GROUP BY requser
UNION ALL
SELECT tblname,
       collect_set(requser) unique_tbls,
       max(later_access_time) latest_time,
       min(early_access_time) earist_time,
       "tableDim" AS dimType,
       '{{ execution_date | cst_ds_nodash }}'
FROM
  (SELECT requser,
          tblname,
          max(evttime) later_access_time,
          min(evttime) early_access_time
   FROM
     (SELECT requser,
             evttime,
             tblname
      FROM jms_audit_info.ranger_audit_info_dt
      WHERE dt='{{ execution_date | cst_ds_nodash }}' ) tt
   GROUP BY requser,
            tblname) tt
GROUP BY tblname